//2023 02.13 17:07 星期一 雨
//--2023 02.18 20:13 星期六 寫完 陰
/**
 * 对 内部 进行排序 进行 阶层
 * @param {Object} styleJson
 * @param {Object} userSequence
 */
function analysisStyleStack(styleEl, userSequence) {
	let stackJson = StackStyle(styleEl)
	let codeSequen = []

	let defaultSequence = [
		["content"],
		["visibility", "display", "opacity", "outline", "overflow", "cursor", "user-select"],
		["position"],
		["vertical-align", "justify-content", "align-items"],
		["z-index"],
		["max-width", "min-width", "max-height", "min-height"],
		["width", "height", "margin", "padding"],
		["margin-top", "margin-right", "margin-bottom", "margin-left"],
		["padding-top", "padding-right", "padding-bottom", "padding-left"],
		["float"],
		["top", "right", "bottom", "left"],
		["outline-color", "outline-offset", "outline-style", "outline-width"],
		["border"],
		["border-top", "border-right", "border-bottom", "border-left", "border-image"],
		["background"],
		["background-attachment", "background-blend-mode", "background-clip", "background-color", "background-image",
			"background-origin", "background-position", "background-position-x", "background-position-y",
			"background-repeat",
			"background-size"
		],
		["font"],
		["font-family", "font-size", "font-style", "font-variant", "font-weight"],
		["color"],
		["transform"],
		["transform-box", "transform-origin", "transform-style"],
		["transition"],
		["transition-property", "transition-duration", "transition-timing-function", "transition-delay"],
		["translate"],
		["float"],
		["animation"],
		["animation-name", "animation-duration", "animation-fill-mode", "animation-timing-function"]
	]




	//是否需要比对??
	if (userSequence != undefined) {
		codeSequen = userSequence
	} else {
		codeSequen = defaultSequence
	}

	//賦值權重
	function weightStackJson(stack) {
		// if (stack["$data"][i].indexOf("@keyframes") > -1) {
		// 	weightStackJson(stack[stack["$data"][i]])
		// }else 
		if (!stack["$digital"]) {
			for (let j = 0; j < stack["$data"].length; j++) {
				weightStackJson(stack[stack["$data"][j]])
			}
		} else {
			stack["$weight"] == undefined ? stack["$weight"] = {} : undefined
			for (let i = 0; i < stack["$data"].length; i++) {
				stack["$weight"][stack["$data"][i]] = locatePosition(stack["$data"][i])
			}
		}
	}


	function releaseWeight(stack) {
		if (!stack["$digital"]) {
			for (let j = 0; j < stack["$data"].length; j++) {
				releaseWeight(stack[stack["$data"][j]])
			}
		} else {
			stack["$releaseW"] == undefined ? stack["$releaseW"] = {} : undefined
			for (let i = 0; i < stack["$data"].length; i++) {
				stack["$releaseW"]["$data"] == undefined ? stack["$releaseW"]["$data"] = [stack["$weight"][stack["$data"][i]][0]] :
					settleWeightArr(stack["$releaseW"]["$data"], stack["$weight"][stack["$data"][i]][0])
				// console.log(stack["$data"][i], stack["$weight"][stack["$data"][i]]);
				stack["$releaseW"][stack["$weight"][stack["$data"][i]][0]] == undefined ?
					stack["$releaseW"][stack["$weight"][stack["$data"][i]][0]] = [stack["$weight"][stack["$data"][i]][1]] :
					settleWeightArr(stack["$releaseW"][stack["$weight"][stack["$data"][i]][0]], stack["$weight"][stack["$data"][i]]
						[1])
			}
		}
	}


	//權重排序
	function settleWeightArr(target, val) {
		let locatIndex = 0
		if (target.indexOf(val) > -1) {
			return false
		} else {
			//從最大數 開始 比對 如果 val 大 則 判定 放置位置 是 i+1 (locatIndex)
			for (let i = target.length - 1; i >= 0; i--) {
				if (target[i] < val) {
					locatIndex = i + 1
					break;
				}
			}
			//从length 开始后挪数据 当到达 locatIndex 时停止变动
			for (let j = target.length; j > locatIndex; j--) {
				target[j] = target[j - 1]
			}
			target[locatIndex] = val
		}
	}

	//定位权重
	function locatePosition(str) {
		for (let i = 0; i < codeSequen.length; i++) {
			if (codeSequen[i].indexOf(str) > -1) {
				let stairs = i
				let index = codeSequen[i].indexOf(str)
				console.log(str, stairs, index);
				return [stairs, index]
			}
		}
	}

	// 根據權重 推演 排序 輸出
	function stackToString(stack, deep) {
		deep == undefined ? deep = 0 : undefined
		let strjoin = ""
		if (!stack["$digital"]) {
			for (let i = 0; i < stack["$data"].length; i++) {
				formPrefix(deep)
				strjoin += stack["$data"][i]

				strjoin += " "

				strjoin += '{'
				strjoin += "\n"

				strjoin += stackToString(stack[stack["$data"][i]], deep + 1)

				formPrefix(deep)

				strjoin += '}'
				strjoin += "\n"
			}

		} else {
			let seqedProps = []
			for (let m = 0; m < stack["$releaseW"]["$data"].length; m++) {
				m == 0 ? undefined : strjoin += "\n"
				let x = stack["$releaseW"]["$data"][m]

				for (let n = 0; n < stack["$releaseW"][x].length; n++) {
					let targetKey = defaultSequence[x][stack["$releaseW"][x][n]]
					if (stack["$data"].indexOf(targetKey) > -1) {
						seqedProps.push(targetKey)
						//核心
						formPrefix(deep)
						strjoin += targetKey
						strjoin += ":"
						strjoin += " "

						strjoin += stack[targetKey]

						strjoin += ';\n'
						//核心
					}
				}
			}
			for (var i = 0; i < stack["$data"].length; i++) {
				if (seqedProps.indexOf(stack["$data"][i]) == -1) {
					//核心
					formPrefix(deep)
					strjoin += stack["$data"][i]
					strjoin += ":"
					strjoin += " "

					strjoin += stack[stack["$data"][i]]

					strjoin += ';\n'
					//核心
				}
			}
		}

		function formPrefix(deep) {
			for (var i = 0; i < deep; i++) {
				strjoin += "\t"
			}
		}
		return strjoin
	}

	weightStackJson(stackJson)
	releaseWeight(stackJson)
	console.log(stackToString(stackJson, 0));
	return stackJson
}
let releaseweightResult = analysisStyleStack(document.getElementById("static_bos"))
console.log(releaseweightResult);


// function settleWeightArr(target, val) {
// 	let locatIndex = 0
// 	if (target.indexOf(val) > -1) {
// 		return false
// 	} else {
// 		//從最大數 開始 比對 如果 val 大 則 判定 放置位置 是 i+1 (locatIndex)
// 		for (let i = target.length - 1; i >= 0; i--) {
// 			if (target[i] < val) {
// 				locatIndex = i + 1
// 				break;
// 			}
// 		}
// 		//从length 开始后挪数据 当到达 locatIndex 时停止变动
// 		for (let j = target.length; j > locatIndex; j--) {
// 			target[j] = target[j - 1]
// 		}
// 		target[locatIndex] = val
// 	}
// }
// let art = []
// let chat = [2, 10, 19, 6, 15, 0, 1]
// for (let i = 0; i < chat.length; i++) {
// 	settleWeightArr(art, chat[i])
// }
